package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.MyHomeActivityV2;
import com.runen.infinitemovement.model.entity.CommentDetailListBean;
import com.runen.infinitemovement.model.entity.ReplyCommentListBean;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.goodview.GoodView;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.runen.rxnetlibrary.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on 2020/09/03 10:55
 */
public class CommentDetailsAdapter extends BaseQuickAdapter<CommentDetailListBean, BaseViewHolder> {
    private Context context;

    public OnItemCommentClick getOnItemCommentClick() {
        return onItemCommentClick;
    }

    public void setOnItemCommentClick(OnItemCommentClick onItemCommentClick) {
        this.onItemCommentClick = onItemCommentClick;
    }

    private OnItemCommentClick onItemCommentClick;

    public CommentDetailsAdapter(Context context) {
        super(R.layout.item_comment_details);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, CommentDetailListBean item) {
        RecyclerView rv_comment_info = helper.getView(R.id.rv_comment_info);
        RoundedCornersImage rci_user_header = helper.getView(R.id.rci_user_header);
        GlideUtils.loadImage(context, item.headImgUrl + "?x-oss-process=image/resize,m_fill,w_144,h_144", rci_user_header);

        TextView tv_user_praise_sum = helper.getView(R.id.tv_user_praise_sum);
        tv_user_praise_sum.setText(String.valueOf(item.likeNumber));
        TextView tv_myself_comment = helper.getView(R.id.tv_myself_comment);
//        String user_content_info;
//        user_content_info = "<font color='#333333'>" + item.nickname + "</font>" + "：";
        helper.setText(R.id.tv_userName, item.nickname);
        String str = "%s";
        tv_myself_comment.setText(Html.fromHtml(String.format(str, item.content)));
        ImageView ivPraisePraise = helper.getView(R.id.iv_user_praise);
        helper.setText(R.id.tv_comment_time, TimeUtil.converFromSecondTime(TimeUtil.getDateTimeToLong(TimeUtil.dealDateFormat1(item.createTime))));
        if (item.isLike) {
            item.isLike = true;
            GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_praised), ivPraisePraise);
            tv_user_praise_sum.setTextColor(CommonUtil.getColor(R.color.color_FFF55656));
        } else {
            item.isLike = false;
            GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_like), ivPraisePraise);
            tv_user_praise_sum.setTextColor(CommonUtil.getColor(R.color.color_FFBCBCBC));
        }
        helper.getView(R.id.ll_praise_sum).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.isLike) {
                    item.isLike = false;
                    item.likeNumber = item.likeNumber - 1;
                    GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_like), ivPraisePraise);
                    tv_user_praise_sum.setTextColor(CommonUtil.getColor(R.color.color_FFBCBCBC));
                } else {
                    item.isLike = true;
                    item.likeNumber = item.likeNumber + 1;
                    GlideUtils.loadImage(context, CommonUtil.getDrawable(R.mipmap.ico_praised), ivPraisePraise);
                    GoodView goodView = new GoodView(context);
                    goodView.setImage(R.mipmap.ico_praised);
                    goodView.show(ivPraisePraise);
                    tv_user_praise_sum.setTextColor(CommonUtil.getColor(R.color.color_FFF55656));
                }
                helper.setText(R.id.tv_user_praise_sum, String.valueOf(item.likeNumber));
                if (null != onItemCommentClick) {
                    onItemCommentClick.onItemPraise(item);
                }
            }

        });
        rci_user_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MyHomeActivityV2.class);
                intent.putExtra("userId", item.reviewerId);
                context.startActivity(intent);
            }
        });

        tv_myself_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemCommentClick) {
                    onItemCommentClick.onItemCommentReply(item, item.commentId);
                }
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rv_comment_info.setLayoutManager(layoutManager);
        CommentMessageAdapter commentMessageAdapter = new CommentMessageAdapter(context);
        rv_comment_info.setAdapter(commentMessageAdapter);
        List<ReplyCommentListBean> listBeans = new ArrayList<>();
        if (item.secComments != null && item.secComments.size() > 0) {
            for (int i = 0; i < item.secComments.size(); i++) {
                listBeans.add(item.secComments.get(i));
            }
            if (item.secCommentsNumber >= 2) {
                ReplyCommentListBean replyCommentListBean = new ReplyCommentListBean();
                String check = "查看全部 %s 条回复";
                replyCommentListBean.reviewerName = "CHECK_ALL";
                replyCommentListBean.content = String.format(check, item.secCommentsNumber);
                listBeans.add(replyCommentListBean);
            }
        }
        commentMessageAdapter.addData(listBeans);
        commentMessageAdapter.setOnSecondItemClick(new CommentMessageAdapter.OnSecondItemClick() {
            @Override
            public void onItemClick(ReplyCommentListBean secondItem) {
                if (onItemCommentClick != null) {
                    onItemCommentClick.onItemSecondReply(secondItem, item.commentId);
                }
            }

            @Override
            public void onMoreClick() {
                if (null != onItemCommentClick) {
                    onItemCommentClick.onItemMore(item, item.commentId);
                }
            }
        });
    }

    public interface OnItemCommentClick {
        /**
         * item中的一条信息点赞
         *
         * @param item
         */
        void onItemPraise(CommentDetailListBean item);

        /**
         * item中的一条信息进行回复
         *
         * @param item
         */
        void onItemCommentReply(CommentDetailListBean item, String topCommentId);

        /**
         * item中二级进行回复
         *
         * @param item
         */
        void onItemSecondReply(ReplyCommentListBean item, String topCommentId);

        /**
         * 跳到更多
         *
         * @param item
         * @param topCommentId
         */
        void onItemMore(CommentDetailListBean item, String topCommentId);
    }
}
